var http = require('../../utils/http')
var utils = require('../../utils/util')
Page({
    data: {
        queryInfo: {
            //页码
            pageNumber: 1,
            //页数
            pageSize: 14,
            //
            queryString: ''
        },
        tableList: [],
        //上拉时时候还有更多要展示的数据
        hasMoreDate: true,
        navHeight: '',
        menuButtonInfo: {},
        searchMarginTop: 0, // 搜索框上边距
        searchWidth: 0, // 搜索框宽度
        searchHeight: 0, // 搜索框高度
    },
    toDetails(event) {
        const item = event.target.dataset.item;
        wx.navigateTo({
            url: `/pages/course/details/details?item=` + encodeURIComponent(JSON.stringify(item)),
        })
    },
    findPage(message) {
        if (!this.data.hasMoreDate) {
            wx.showNavigationBarLoading();
            wx.showToast({
                title: message,
                icon: 'loading',
                duration: 1000
            })
        }
        const that = this;
        const token = wx.getStorageSync('token')
        if(token != ''){
            http.post("/course/findPage",this.data.queryInfo,res=>{
                if (res.statusCode == 401) {
                    wx.clearStorageSync()
                    that.findPage("数据加载中。。。")
                }else{
                    //将原有的结果列表定义为临时数组
                    let temp = this.data.tableList;
                    //将新的结果接收
                    let result1 = res.data.rows;
                    console.log(result1);
                    let result = result1.map((item, index) => {
                        if (item != null) {
                            item.createTime = utils.formatTime(item);
                        }
                        return Object.assign(item)
                    })
                    utils.pageHelper(result,temp,this)
                }
            })
        }else{
            http.postNoHeader("/course/findPage",this.data.queryInfo,res=>{
                //将原有的结果列表定义为临时数组
                let temp = this.data.tableList;
                //将新的结果接收
                let result1 = res.data.rows;
                console.log(result1);
                let result = result1.map((item, index) => {
                    if (item != null) {
                        item.createTime = utils.formatTime(item);
                    }
                    return Object.assign(item)
                })
                utils.pageHelper(result,temp,this)
            })
        }       
        
    },
    onLoad: function (options) {
        this.setData({
            menuButtonInfo: wx.getMenuButtonBoundingClientRect()
          })
        //   console.log(this.data.menuButtonInfo)
          const { top, width, height, right } = this.data.menuButtonInfo
          wx.getSystemInfo({
            success: (res) => {
              const { statusBarHeight } = res
              const margin = top - statusBarHeight
              this.setData({
                navHeight: (height + statusBarHeight + (margin * 2)),
                searchMarginTop: statusBarHeight + margin, // 状态栏 + 胶囊按钮边距
                searchHeight: height,  // 与胶囊按钮同高
                searchWidth: right - width // 胶囊按钮右边坐标 - 胶囊按钮宽度 = 按钮左边可使用宽度
              })
            },
          })
        // 生命周期函数--监听页面加载
        this.findPage('加载中....');
    },
    bindblur(e){
        this.setData({
            'queryInfo.pageNumber':1,
            'queryInfo.queryString':e.detail.value
        })
        this.findPage("数据加载中。。。")
        
    },
    onShow: function () {
        // 生命周期函数--监听页面显示
        this.getTabBar().init();
    },
    onPullDownRefresh: function () {
        // 页面相关事件处理函数--监听用户下拉动作
        this.setData({
            'queryInfo.pageNumber':1,
            tableList:[]
        })
        this.findPage('数据刷新中....');
    },
    onReachBottom: function () {
        // 页面上拉触底事件的处理函数
        this.findPage('数据加载中....')
    }
})